References

#1#


<#376#>Appendix A Estimating Pythagorean Square-root<#376#>


For the line drawing commands described in the main sections of this document, we need to estimate the Pythagorean square-root in order to determine the length of the line (along its slope). More precisely, we need to estimate the number of segments of a given length needed to draw a line. TEX does not provide for floating point calculations, and thus there are no direct means of calculating the above square-root. Most standard numerical techniques are iterative and would be too slow when used with TEX for lack of floating point calculations, and in particular, real division, since calculation of such a square-root is needed very frequently.

A simple non-iterative formula for estimating the square-root is derived and described below.


<#377#>Problem: <#377#> Given a and b, to find c = #math95##tex2html_wrap_inline1741# using only operations in {+ , - ,*,/}.

We can get very tight bounds on the square-root as follows. Without loss of generality, let ab. We seek a simple n such that:

#math96#

#tex2html_wrap_indisplay1746#≥a + #tex2html_wrap_indisplay1747#

Squaring both sides, we have

#math97#

a2 + b2 a2 + #tex2html_wrap_indisplay1753# + #tex2html_wrap_indisplay1754#
(1 - #tex2html_wrap_indisplay1757#)b2 #tex2html_wrap_indisplay1760#
#tex2html_wrap_indisplay1763# #tex2html_wrap_indisplay1766#
<#1#>or <#1#> (#tex2html_wrap_indisplay1769#)n2 -2n - (#tex2html_wrap_indisplay1770#) 0

;SPMgt;From the quadratic equation above, we finally get an expression for n,

#math98#

n   =  #tex2html_wrap_indisplay1775#   =  #tex2html_wrap_indisplay1776#

Only the +ve root interests us since n has to be positive. Note that the term under the root is bounded above and below (since #math99##tex2html_wrap_inline1780#≤1):

#math100#

1  ≤  #tex2html_wrap_indisplay1782#  ≤  #tex2html_wrap_indisplay1783#

Hence, we have two values for n,

#math101#

nl   =  #tex2html_wrap_indisplay1786#   =  #tex2html_wrap_indisplay1787#;                    nu   =  #tex2html_wrap_indisplay1788#   =  #tex2html_wrap_indisplay1789#

which finally gives us a lower and an upper bound for c, the Pythagorean square-root,

#math102#

a + #tex2html_wrap_indisplay1792#  ≤  c  ≤  a + #tex2html_wrap_indisplay1793#

These are very tight bounds. Denoting the lower bound as cl and upper one cu, below are some numerical results (c = exact square-root):

a b c cl cu
100.0 100.0 141.4213 141.4213 150.0
100.0 ;SPMnbsp;80.0 128.0642 126.5096 132.0
;SPMnbsp;30.0 ;SPMnbsp;20.0 ;SPMnbsp;36.0555 35.5228 36.6667

With the above bounds, one can do a linear interpolation to get exact values. In our case, since it is not required to be <#434#>extremely<#434#> accurate, for estimating the square-root in the line drawing commands, we simply take the midpoint of the two bounds. For small numbers, which is expected to be the case most of the time, the error is very small.

With some algebra, we get the mid-point estimate of c,

#math103#

c = #tex2html_wrap_indisplay1821# = a + #tex2html_wrap_indisplay1822# = a + #tex2html_wrap_indisplay1823#        (ab)

The macro 144 uses the above formula for estimating the number of points (for 145 macro) and number of segments (for 146 macro). The 147 macro, instead of calculating the length of the line, directly calculates the <#441#>number<#441#> of segments of a given length. For example, to draw a dotted line from (x1, y1) to (x2, y2) with the inter-dot-gap as d, we estimate the number of dots n using the following expression,

#math104#

n = #tex2html_wrap_indisplay1829# + #tex2html_wrap_indisplay1830#              Δx = | x2 - x1|<#1#> and <#1#>Δy = | y2 - y1|

assuming #math105#ΔxΔy (otherwise they may be inter-changed).

Note that since divisions in TEX are integer-divisions, it is simpler to deal in ``number of segments'' rather than actual lengths (e.g. in the expression above, #math106##tex2html_wrap_inline1833# = number of segments along X-axis).

<#451#>Caveat:<#451#> The approach presented here for estimation of Pythagorean square-root is an independent effort by the author. It may already exist in the literature --- the author is neither aware of it nor has he made any serious attempts at uncovering it.